﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LibFriesland_Campina_CRM
{
    public class BizAttendanceTracking
    {
        #region Thuoc tinh
        public long attendance_tracking_numid { get; set; }

        public DateTime? datetime_phone { get { return Thuvien.myConvertStringToLongDate(datetime_phone_str); } }
        public DateTime? datetime_system { get { return Thuvien.myConvertStringToLongDate(datetime_system_str); } }
        public DateTime? datetime_gps { get { return Thuvien.myConvertStringToLongDate(datetime_gps_str); } }

        public string datetime_phone_str { get; set; }
        public string datetime_system_str { get; set; }
        public string datetime_gps_str { get; set; }

        public double latitude { get; set; }
        public double longitude { get; set; }

        public DateTime? datetime_monitoring { get { return Thuvien.myConvertStringToLongDate(datetime_monitoring_str); } }
        public string datetime_monitoring_str { get; set; }

        public string action_monitoring { get; set; }
        public string comment_monitoring { get; set; }
        public string by_na { get; set; }
        public string by_shift { get; set; }
        public string by_time_point { get; set; }
        public string by_sup { get; set; }
        public string by_shop { get; set; }

        //Bien kiem tra toa do cham cong va toa do shop
        public bool latlng_status { get; set; }
        #endregion

        #region Ham thiet lap
        public BizAttendanceTracking() { }
        public BizAttendanceTracking(long pAttendance_tracking_numid, string pDatetime_phone_str, string pDatetime_system_str, string pDatetime_gps_str, double pLatitude, double pLongitude, string pDatetime_monitoring_str, string pAction_monitoring, string pComment_monitoring, string pBy_na, string pBy_shift, string pBy_time_point, string pBy_shop) 
        {
            attendance_tracking_numid = pAttendance_tracking_numid;
            datetime_phone_str = pDatetime_phone_str;
            datetime_system_str = pDatetime_system_str;
            datetime_gps_str = pDatetime_gps_str;
            latitude = pLatitude;
            longitude = pLongitude;
            datetime_monitoring_str = pDatetime_monitoring_str;
            action_monitoring = pAction_monitoring;
            comment_monitoring = pComment_monitoring;
            by_na = pBy_na;
            by_shift = pBy_shift;
            by_time_point = pBy_time_point;
            by_shop = pBy_shop;
        }
        public BizAttendanceTracking(long pAttendance_tracking_numid, string pDatetime_system_str, double pLatitude, double pLongitude, string pDatetime_monitoring_str, string pAction_monitoring, string pComment_monitoring, string pBy_na, string pBy_shift, string pBy_time_point, bool pLatlng_status)
        {
            attendance_tracking_numid = pAttendance_tracking_numid;
            datetime_system_str = pDatetime_system_str;
            latitude = pLatitude;
            longitude = pLongitude;
            datetime_monitoring_str = pDatetime_monitoring_str;
            action_monitoring = pAction_monitoring;
            comment_monitoring = pComment_monitoring;
            by_na = pBy_na;
            by_shift = pBy_shift;
            by_time_point = pBy_time_point;
            latlng_status = pLatlng_status;
        }
        public BizAttendanceTracking(long pAttendance_tracking_numid, string pDatetime_phone_str, string pDatetime_system_str, string pDatetime_gps_str, double pLatitude, double pLongitude, string pBy_na, string pBy_shift, string pBy_time_point)
        {
            attendance_tracking_numid = pAttendance_tracking_numid;
            datetime_phone_str = pDatetime_phone_str;
            datetime_system_str = pDatetime_system_str;
            datetime_gps_str = pDatetime_gps_str;
            latitude = pLatitude;
            longitude = pLongitude;
            by_na = pBy_na;
            by_shift = pBy_shift;
            by_time_point = pBy_time_point;
        }
        #endregion

        #region Phuong thuc
        //Webservice
        //Them theo doi cham cong cua mot NA
        public int addAttendanceTracking()
        {
            return DalAttendanceTracking.addAttendanceTracking(this);
        }

        //Webservice
        //Lay thong tin theo doi cham cong cua NA theo ngay
        public static List<BizAttendanceTracking> getAttendanceTrackingbyNA(string pBy_na,DateTime pDatetime_system,  string pShop_charid, string pShift_charid)
        {
            return DalAttendanceTracking.getAttendanceTrackingbyNA(pBy_na, pDatetime_system, pShop_charid, pShift_charid);
        }

        //Lay thong tin cham cong cua NA theo ngay: FULL
        public static List<BizAttendanceTracking> getFullAttendanceTrackingbyNA(string pBy_na, DateTime pDatetime_system, string pShop_charid, string pShift_charid)
        {
            return DalAttendanceTracking.getFullAttendanceTrackingbyNA(pBy_na, pDatetime_system, pShop_charid, pShift_charid);
        }

        //Webservice
        //Cap nhat thong tin theo doi cham cong cua Supervisor
        public static int updateAttendanceTracking(int pAttendance_tracking_numid, DateTime pDatetime_monitoring, string pAction_monitoring, string pComment_monitoring)
        {
            return DalAttendanceTracking.updateAttendanceTracking(pAttendance_tracking_numid, pDatetime_monitoring, pAction_monitoring, pComment_monitoring);
        }
         
        //Kiem tra ton tai thong tin theo doi cham cong cua NA: by_shift - by_time_point - datetime_system
        public static int isExistAttendanceTracking(string pBy_na, string pBy_shift, string pBy_time_point, DateTime pDatetime_system)
        {
            return DalAttendanceTracking.isExistAttendanceTracking(pBy_na, pBy_shift, pBy_time_point, pDatetime_system);
        }

        //Kiem tra toa do cham cong co phu hop voi toa do shop
        public bool getLatlngStatus()
        {
            return DalAttendanceTracking.getLatlngStatus(this.by_shop, this.latitude, this.longitude);
        }

        //Kiem tra dung gio cham cong theo Shift, TimePoint
        public int getTimeStatus()
        {
            int result = 0;
            BizShift shift = DalShift.getShiftbyID(this.by_shift);
            //Dau gio
            if (this.by_time_point == "1")
            {
                DateTime dt = Convert.ToDateTime(this.datetime_phone);
                //DateTime dt = Convert.ToDateTime(this.datetime_system);
                double min = (dt.TimeOfDay.Subtract(shift.starttime.TimeOfDay)).TotalMinutes;
                if (min <= 15)
                    result = 1;
                else
                    result = 0;
            }

            //Giua gio
            if (this.by_time_point == "2")
            {
                DateTime dt = Convert.ToDateTime(this.datetime_phone);
                //DateTime dt = Convert.ToDateTime(this.datetime_system);
                double min = (dt.TimeOfDay.Subtract(shift.middletime.TimeOfDay)).TotalMinutes;
                if (min <= 15)
                    result = 1;
                else
                    result = 0;
            }

            //Cuoi gio
            if (this.by_time_point == "3")
            {
                DateTime dt = Convert.ToDateTime(this.datetime_phone);
                //DateTime dt = Convert.ToDateTime(this.datetime_system);
                double min = (dt.TimeOfDay.Subtract(shift.middletime.TimeOfDay)).TotalMinutes;
                if (min >= -15)
                    result = 1;
                else
                    result = 0;
            }
            return result;
        }
        #endregion
    }
}
